package com.tbm.console.proj.json;

import com.tbm.console.base.controller.BaseController;
import com.tbm.console.common.constant.InsUpdDelFlagConstant;
import com.tbm.console.proj.entity.EquipmentEntity;
import com.tbm.console.proj.service.EquipmentService;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class EquipmentControllerJson extends BaseController{

    @Autowired
    private EquipmentService equipmentService;

    @RequestMapping(value = "/equip-list-json/{start}/{length}")
    public Map<String,Object> equipmentList(@PathVariable("start") Integer start, @PathVariable("length") Integer length){
        PageData pageData = this.getPageData();
        PageData param = new PageData();
        param.put("deleteFlag", InsUpdDelFlagConstant.DELETE_FLAG_N);
        if(null!=pageData.get("equipType")&&!"".equals(pageData.getString("equipType"))){
            param.put("equipType", pageData.getString("equipType"));
        }
        if(null!=pageData.get("equipBelongTo")&&!"".equals(pageData.getString("equipBelongTo"))){
            param.put("equipBelongTo", pageData.getString("equipBelongTo"));
        }
        if(null!=pageData.get("equipRentState")&&!"".equals(pageData.getString("equipRentState"))){
            param.put("equipRentState", pageData.getString("equipRentState"));
        }

        List<EquipmentEntity> equipmentEntities = equipmentService.findAll(param,start==0?0:start-1,length);
        Long count = equipmentService.count(param);
        pageData.put("total", count);
        pageData.put("rows", equipmentEntities);
        return pageData;
    }
    @RequestMapping(value = "/equipment-save/{id}")
    public String equipmentSave(@PathVariable("id") String id){
        PageData pd = this.getPageData();
        EquipmentEntity equipmentEntity = equipmentService.findById(Integer.valueOf(id));
        if(null!=pd.get("equipName")&&!"".equals(pd.getString("equipName"))){
            equipmentEntity.setEquipName(pd.getString("equipName"));
        }
        if(null!=pd.get("equipType")&&!"".equals(pd.getString("equipType"))){
            equipmentEntity.setEquipType(pd.getString("equipType"));
        }
        if(null!=pd.get("equipSpecifications")&&!"".equals(pd.getString("equipSpecifications"))){
            equipmentEntity.setEquipSpecifications(pd.getString("equipSpecifications").replaceAll("\r|\n","").replaceAll("\n",""));
        }
        if(null!=pd.get("equipRentUnit")&&!"".equals(pd.getString("equipRentUnit"))){
            equipmentEntity.setEquipRentUnit(pd.getString("equipRentUnit"));
        }
        if(null!=pd.get("equipFactory")&&!"".equals(pd.getString("equipFactory"))){
            equipmentEntity.setEquipFactory(pd.getString("equipFactory"));
        }
        if(null!=pd.get("equipPurchaseTime")&&!"".equals(pd.getString("equipPurchaseTime"))){
            equipmentEntity.setEquipPurchaseTime(pd.getString("equipPurchaseTime"));
        }
        if(null!=pd.get("equipBelongTo")&&!"".equals(pd.getString("equipBelongTo"))){
            equipmentEntity.setEquipBelongTo(pd.getString("equipBelongTo"));
        }
        if(null!=pd.get("equipUsedLocation")&&!"".equals(pd.getString("equipUsedLocation"))){
            equipmentEntity.setEquipUsedLocation(pd.getString("equipUsedLocation"));
        }
        if(null!=pd.get("equipLocaltion")&&!"".equals(pd.getString("equipLocaltion"))){
            equipmentEntity.setEquipLocaltion(pd.getString("equipLocaltion"));
        }
        if(null!=pd.get("equipState")&&!"".equals(pd.getString("equipState"))){
            equipmentEntity.setEquipState(pd.getString("equipState"));
        }
        if(null!=pd.get("equipRentState")&&!"".equals(pd.getString("equipRentState"))){
            equipmentEntity.setEquipRentState(pd.getString("equipRentState"));
        }
        if(null!=pd.get("equipTechnology")&&!"".equals(pd.getString("equipTechnology"))){
            equipmentEntity.setEquipTechnology(pd.getString("equipTechnology"));
        }
        if(null!=pd.get("equipPurchaseMoney")&&!"".equals(pd.getString("equipPurchaseMoney"))){
            equipmentEntity.setEquipPurchaseMoney(pd.getString("equipPurchaseMoney"));
        }
        if(null!=pd.get("deleteFlag")&&!"".equals(pd.getString("deleteFlag"))){
            equipmentEntity.setDeleteFlag(InsUpdDelFlagConstant.DELETE_FLAG_Y);
        }
        equipmentService.save(equipmentEntity);
        return "success";
    }
}
